from __future__ import annotations
from enum import Enum
from app.api.schemas.languages import Lang


class BaseLang(Enum):
    """定义集成的翻译语言类"""

    def __new__(cls, lang: Lang, code: str):
        obj = object.__new__(cls)
        obj._value_ = code
        obj._lang = lang
        obj._code = code
        return obj

    @property
    def lang(self) -> Lang:
        return self._lang

    @property
    def code(self) -> str:
        return self._code

    @classmethod
    def from_code(cls, code: str) -> BaseLang:
        return next(
            (lan for lan in cls if lan.code == code),
            next(lan for lan in cls if lan.code == "en"),
        )

    # for lang_item in cls:
    #     if lang_item.code == code:
    #         return lang_item
    # raise ValueError(f"不支持的语言编码: {code}")

    @classmethod
    def from_lang(cls, lang: Lang) -> BaseLang:
        return next(
            (lan for lan in cls if lan.lang == lang),
            next(lan for lan in cls if lan.lang == Lang.EN),
        )

    @classmethod
    def to_code(cls, lang: BaseLang) -> str:
        return cls.methods.to_code(lang)
